//
//  UserNavigationController.m
//  wangfenguser
//
//  Created by wj on 16/4/4.
//  Copyright © 2016年 wj. All rights reserved.
//

#import "UserNavigationController.h"

@interface UserNavigationController ()<UIGestureRecognizerDelegate,UINavigationControllerDelegate>

@end

@implementation UserNavigationController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    //设置UINavigationBar的主题
    [self setupNavigationBarTheme];
}

-(void)setupNavigationBarTheme
{
    //渐变
    [self.navigationBar setBackgroundImage:[self gradientLayer] forBarMetrics:UIBarMetricsDefault];
    
    //设置文字属性
    NSMutableDictionary *textAttrs=[NSMutableDictionary dictionary];
    //设置字体颜色
    textAttrs[NSForegroundColorAttributeName] = CSWhiteColor;
    //设置字体
    textAttrs[NSFontAttributeName]= [UIFont boldSystemFontOfSize:16];
    //设置字体的偏移量（0）
    //说明：UIOffsetZero是结构体，只有包装成NSValue对象才能放进字典中
    [self.navigationBar setTitleTextAttributes:textAttrs];
    
   
    
    
    self.navigationBar.shadowImage = [UIImage new];
    
    
    
    self.navigationBar.translucent = NO;
}



/**
 导航栏颜色渐变

 @return CAGradientLayer
 */



- (UIImage *)gradientLayer {
    
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, adaptNavagationBar)];
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[
                             (__bridge id)CSColorFromRGB(0xF52C50).CGColor,
                             (__bridge id)CSColorFromRGB(0xF95C48).CGColor,
                             
                             ];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1.0, 0);
    gradientLayer.frame = backView.frame;
    [backView.layer insertSublayer:gradientLayer atIndex:0];
    
    CGSize s = backView.bounds.size;
    // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需  要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了
    UIGraphicsBeginImageContextWithOptions(s, YES, [UIScreen mainScreen].scale);
    [backView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
    
    
    
}

#pragma mark 重写父类的方法
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    
    if (self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
        UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"icon_return"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
        viewController.navigationItem.leftBarButtonItem = leftButtonItem;
    }
    
    [super pushViewController:viewController animated:YES];
    
    // 修改tabBra的frame
    CGRect frame = self.tabBarController.tabBar.frame;
    frame.origin.y = [UIScreen mainScreen].bounds.size.height - frame.size.height;
    self.tabBarController.tabBar.frame = frame;
}

/**
 *  返回方法
 *
 */
-(void)back
{

    [self popViewControllerAnimated:YES];
}


/**
 *  生成图片
 *
 *  @param color  图片颜色
 *  @param height 图片高度
 *
 *  @return 生成的图片
 */
- (UIImage*) GetImageWithColor:(UIColor*)color andHeight:(CGFloat)height
{
    CGRect r= CGRectMake(0.0f, 0.0f, 1.0f, height);
    UIGraphicsBeginImageContext(r.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, r);
    
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return img;
}

@end
